home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gdb / gdb_18s.zoo / stack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  16.6 KB  |  621 lines

  1. /* Print and select stack frames for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3.  
  4. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the GDB General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute GDB,
  11. but only under the conditions described in the GDB General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with GDB so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17. In other words, go ahead and share GDB, but don't try to stop
  18. anyone else from sharing it farther.  Help stamp out software hoarding!
  19. */
  20.  
  21. #include <stdio.h>
  22.  
  23. #include "defs.h"
  24. #include "param.h"
  25. #include "symtab.h"
  26. #include "frame.h"
  27.  
  28. /* Thie "selected" stack frame is used by default for local and arg access.
  29.    May be zero, for no selected frame.  */
  30.  
  31. FRAME selected_frame;
  32.  
  33. /* Level of the selected frame:
  34.    0 for innermost, 1 for its caller, ...
  35.    or -1 for frame specified by address with no defined level.  */
  36.  
  37. int selected_frame_level;
  38.  
  39. /* Nonzero means print the full filename and linenumber
  40.    when a frame is printed, and do so in a format programs can parse.  */
  41.  
  42. int frame_file_full_name = 0;
  43.  
  44. static void select_calling_frame ();
  45.  
  46. void print_frame_info ();
  47.  
  48. /* Print a stack frame briefly.  FRAME should be the frame address
  49.    and LEVEL should be its level in the stack (or -1 for level not defined).
  50.    This prints the level, the function executing, the arguments,
  51.    and the file name and line number.
  52.    If the pc is not at the beginning of the source line,
  53.    the actual pc is printed at the beginning.
  54.  
  55.    If SOURCE is 1, print the source line as well.
  56.    If SOURCE is -1, print ONLY the source line.  */
  57.  
  58. static void
  59. print_stack_frame (frame, level, source)
  60.      FRAME frame;
  61.      int level;
  62.      int source;
  63. {
  64.   struct frame_info fi;
  65.  
  66.   fi = get_frame_info (frame);
  67.  
  68.   print_frame_info (&fi, level, source, 1);
  69. }
  70.  
  71. void
  72. print_frame_info (fi, level, source, args)
  73.      struct frame_info *fi;
  74.      register int level;
  75.      int source;
  76.      int args;
  77. {
  78.   register FRAME frame = fi->frame;
  79.   struct symtab_and_line sal;
  80.   struct symbol *func;
  81.   register char *funname = 0;
  82.   int numargs;
  83.  
  84.   sal = find_pc_line (fi->pc, fi->next_frame);
  85.   func = get_frame_function (frame);
  86.   if (func)
  87.     funname = SYMBOL_NAME (func);
  88.   else
  89.     {
  90.       register int misc_index = find_pc_misc_function (fi->pc);
  91.       if (misc_index >= 0)
  92.     funname = misc_function_vector[misc_index].name;
  93.     }
  94.  
  95.   if (source >= 0 || !sal.symtab)
  96.     {
  97.       /* This avoids a bug in cc on the sun.  */
  98.       struct frame_info tem;
  99.       tem = *fi;
  100.  
  101.       if (level >= 0)
  102. printf_filtered ("#%-2d ", level);
  103.       if (fi->pc != sal.pc || !sal.symtab)
  104. printf_filtered ("0x%x in ", fi->pc);
  105.      printf_filtered ("%s (", funname ? funname : "??");
  106.       if (args)
  107.     {
  108.       FRAME_NUM_ARGS (numargs, tem);
  109.       print_frame_args (func, FRAME_ARGS_ADDRESS (tem), numargs, stdout);
  110.     }
  111.      printf_filtered (")");
  112.       if (sal.symtab)
  113. printf_filtered (" (%s line %d)", sal.symtab->filename, sal.line);
  114.      printf_filtered ("\n");
  115.     }
  116.  
  117.   if (source != 0 && sal.symtab)
  118.     {
  119.       int done = 0;
  120.       int mid_statement = source < 0 && fi->pc != sal.pc;
  121.       if (frame_file_full_name)
  122.     done = identify_source_line (sal.symtab, sal.line, mid_statement);
  123.       if (!done)
  124.     {
  125.       if (mid_statement)
  126.        printf_filtered ("0x%x\t", fi->pc);
  127.       print_source_lines (sal.symtab, sal.line, sal.line + 1);
  128.     }
  129.       current_source_line = max (sal.line - 5, 1);
  130.     }
  131.   if (source != 0)
  132.     set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
  133.  
  134.   fflush (stdout);
  135. }
  136.  
  137. /* Call here to print info on selected frame, after a trap.  */
  138.  
  139. void
  140. print_sel_frame (just_source)
  141.      int just_source;
  142. {
  143.   print_stack_frame (selected_frame, -1, just_source ? -1 : 1);
  144. }
  145.  
  146. /* Print info on the selected frame, including level number
  147.    but not source.  */
  148.  
  149. print_selected_frame ()
  150. {
  151.   print_stack_frame (selected_frame, selected_frame_level, 0);
  152. }
  153.  
  154. /* Print verbosely the selected frame or the frame at address ADDR.
  155.    This means absolutely all information in the frame is printed.  */
  156.  
  157. static void
  158. frame_info (addr_exp)
  159.      char *addr_exp;
  160. {
  161.   FRAME frame = addr_exp ? parse_and_eval_address (addr_exp) : selected_frame;
  162.   struct frame_info fi;
  163.   struct frame_saved_regs fsr;
  164.   struct symtab_and_line sal;
  165.   struct symbol *func;
  166.   FRAME calling_frame;
  167.   int i, count;
  168.   char *funname = 0;
  169.   int numargs;
  170.  
  171.   fi = get_frame_info (frame);
  172.   get_frame_saved_regs (&fi, &fsr);
  173.   sal = find_pc_line (fi.pc, fi.next_frame);
  174.   func = get_frame_function (frame);
  175.   if (func)
  176.     funname = SYMBOL_NAME (func);
  177.   else
  178.     {
  179.       register int misc_index = find_pc_misc_function (fi.pc);
  180.       if (misc_index >= 0)
  181.     funname = misc_function_vector[misc_index].name;
  182.     }
  183.   calling_frame = get_prev_frame (frame);
  184.  
  185.   if (!addr_exp && selected_frame_level >= 0)
  186.    printf_filtered ("Stack level %d, frame at 0x%x:\n pc = 0x%x",
  187.         selected_frame_level, frame, fi.pc);
  188.   else
  189.    printf_filtered ("Stack frame at 0x%x:\n pc = 0x%x",
  190.         frame, fi.pc);
  191.  
  192.   if (funname)
  193.    printf_filtered (" in %s", funname);
  194.   if (sal.symtab)
  195.    printf_filtered (" (%s line %d)", sal.symtab->filename, sal.line);
  196.  printf_filtered ("; saved pc 0x%x\n", FRAME_SAVED_PC (frame));
  197.   if (calling_frame)
  198.    printf_filtered (" called by frame at 0x%x", calling_frame);
  199.   if (fi.next_frame && calling_frame)
  200.    printf_filtered (",");
  201.   if (fi.next_frame)
  202.    printf_filtered (" caller of frame at 0x%x", fi.next_frame);
  203.   if (fi.next_frame || calling_frame)
  204.    printf_filtered ("\n");
  205.  printf_filtered (" Arglist at 0x%x,", FRAME_ARGS_ADDRESS (fi));
  206.   FRAME_NUM_ARGS (i, fi);
  207.   if (i < 0)
  208.    printf_filtered (" args: ");
  209.   else if (i == 0)
  210.    printf_filtered (" no args.");
  211.   else if (i == 1)
  212.    printf_filtered (" 1 arg: ");
  213.   else
  214.    printf_filtered (" %d args: ", i);
  215.  
  216.   FRAME_NUM_ARGS (numargs, fi);
  217.   print_frame_args (func, FRAME_ARGS_ADDRESS (fi), numargs, stdout);
  218.  printf_filtered ("\n");
  219.   count = 0;
  220.   for (i = 0; i < NUM_REGS; i++)
  221.     if (fsr.regs[i])
  222.       {
  223.     if (count % 4 != 0)
  224.      printf_filtered (", ");
  225.     else
  226.       {
  227.         if (count == 0)
  228.          printf_filtered (" Saved registers:");
  229.        printf_filtered ("\n  ");
  230.       }
  231. printf_filtered ("%s at 0x%x", reg_names[i], fsr.regs[i]);
  232.     count++;
  233.       }
  234.   if (count)
  235.    printf_filtered ("\n");
  236. }
  237.  
  238. /* Print briefly all stack frames or just the innermost COUNT frames.  */
  239.  
  240. static void
  241. backtrace_command (count_exp)
  242.      char *count_exp;
  243. {
  244.   struct frame_info fi;
  245.   register int count;
  246.   register FRAME frame;
  247.   register int i;
  248.  
  249.   if (count_exp)
  250.     count = parse_and_eval_address (count_exp);
  251.   else
  252.     count = -1;
  253.  
  254.   for (i = 0, frame = get_current_frame (), fi = get_frame_info (frame);
  255.        frame && count--;
  256.        i++, fi = get_prev_frame_info (fi.frame), frame = fi.frame)
  257.     {
  258.       QUIT;
  259.       print_frame_info (&fi, i, 0, 1);
  260.     }
  261. }
  262.  
  263. /* Print the local variables of a block B active in FRAME.  */
  264.  
  265. static void
  266. print_block_frame_locals (b, frame, stream)
  267.      struct block *b;
  268.      register FRAME frame;
  269.      register FILE *stream;
  270. {
  271.   int nsyms;
  272.   register int i;
  273.   register struct symbol *sym;
  274.  
  275.   nsyms = BLOCK_NSYMS (b);
  276.  
  277.   for (i = 0; i < nsyms; i++)
  278.     {
  279.       sym = BLOCK_SYM (b, i);
  280.       if (SYMBOL_CLASS (sym) == LOC_LOCAL
  281.       || SYMBOL_CLASS (sym) == LOC_REGISTER
  282.       || SYMBOL_CLASS (sym) == LOC_STATIC)
  283.     {
  284.       fprintf_filtered (stream, "%s = ", SYMBOL_NAME (sym));
  285.       print_variable_value (sym, frame, stream);
  286.       fprintf_filtered (stream, "\n");
  287.       fflush (stream);
  288.     }
  289.     }
  290. }
  291.  
  292. /* Print on STREAM all the local variables in frame FRAME,
  293.    including all the blocks active in that frame
  294.    at its current pc.
  295.  
  296.    Returns 1 if the job was done,
  297.    or 0 if nothing was printed because we have no info
  298.    on the function running in FRAME.  */
  299.  
  300. static int
  301. print_frame_local_vars (frame, stream)
  302.      register FRAME frame;
  303.      register FILE *stream;
  304. {
  305.   register struct block *block = get_frame_block (frame);
  306.   if (block == 0)
  307.     return 0;
  308.   while (block != 0)
  309.     {
  310.       print_block_frame_locals (block, frame, stream);
  311.       /* After handling the function's top-level block, stop.
  312.      Don't continue to its superblock, the block of
  313.      per-file symbols.  */
  314.       if (BLOCK_FUNCTION (block))
  315.     break;
  316.       block = BLOCK_SUPERBLOCK (block);
  317.     }
  318.   return 1;
  319. }
  320.  
  321. static void
  322. locals_info ()
  323. {
  324.   print_frame_local_vars (selected_frame, stdout);
  325. }
  326.  
  327. static int
  328. print_frame_arg_vars (frame, stream)
  329.      register FRAME frame;
  330.      register FILE *stream;
  331. {
  332.   struct symbol *func = get_frame_function (frame);
  333.   register struct block *b;
  334.   int nsyms;
  335.   register int i;
  336.   register struct symbol *sym;
  337.  
  338.   if (func == 0)
  339.     return 0;
  340.  
  341.   b = SYMBOL_BLOCK_VALUE (func);
  342.   nsyms = BLOCK_NSYMS (b);
  343.  
  344.   for (i = 0; i < nsyms; i++)
  345.     {
  346.       sym = BLOCK_SYM (b, i);
  347.       if (SYMBOL_CLASS (sym) == LOC_ARG)
  348.     {
  349.       fprintf_filtered (stream, "%s = ", SYMBOL_NAME (sym));
  350.       print_variable_value (sym, frame, stream);
  351.       fprintf_filtered (stream, "\n");
  352.       fflush (stream);
  353.     }
  354.     }
  355.  
  356.   return 1;
  357. }
  358.  
  359. static void
  360. args_info ()
  361. {
  362.   print_frame_arg_vars (selected_frame, stdout);
  363. }
  364.  
  365. /* Select frame FRAME, and note that its stack level is LEVEL.
  366.    LEVEL may be -1 if an actual level number is not known.  */
  367.  
  368. void
  369. select_frame (frame, level)
  370.      FRAME frame;
  371.      int level;
  372. {
  373.   selected_frame = frame;
  374.   selected_frame_level = level;
  375. }
  376.  
  377. /* Store the selected frame and its level into *FRAMEP and *LEVELP.  */
  378.  
  379. void
  380. record_selected_frame (framep, levelp)
  381.      FRAME *framep;
  382.      int *levelp;
  383. {
  384.   *framep = selected_frame;
  385.   *levelp = selected_frame_level;
  386. }
  387.  
  388. /* Return the symbol-block in which the selected frame is executing.
  389.    Can return zero under various legitimate circumstances.  */
  390.  
  391. struct block *
  392. get_selected_block ()
  393. {
  394.   if (!have_inferior_p () && !have_core_file_p ())
  395.     return 0;
  396.  
  397.   if (!selected_frame)
  398.     return get_current_block ();
  399.   return get_frame_block (selected_frame);
  400. }
  401.  
  402. /* Find a frame a certain number of levels away from FRAME.
  403.    LEVEL_OFFSET_PTR points to an int containing the number of levels.
  404.    Positive means go to earlier frames (up); negative, the reverse.
  405.    The int that contains the number of levels is counted toward
  406.    zero as the frames for those levels are found.
  407.    If the top or bottom frame is reached, that frame is returned,
  408.    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
  409.    how much farther the original request asked to go.  */
  410.  
  411. FRAME
  412. find_relative_frame (frame, level_offset_ptr)
  413.      register FRAME frame;
  414.      register int* level_offset_ptr;
  415. {
  416.   register FRAME prev;
  417.   struct frame_info fi;
  418.   register FRAME frame1, frame2;
  419.  
  420.   /* Going up is simple: just do get_prev_frame enough times
  421.      or until initial frame is reached.  */
  422.   while (*level_offset_ptr > 0)
  423.     {
  424.       prev = get_prev_frame (frame);
  425.       if (prev == 0)
  426.     break;
  427.       (*level_offset_ptr)--;
  428.       frame = prev;
  429.     }
  430.   /* Going down could be done by iterating get_frame_info to
  431.      find the next frame, but that would be quadratic
  432.      since get_frame_info must scan all the way from the current frame.
  433.      The following algotithm is linear.  */
  434.   if (*level_offset_ptr < 0)
  435.     {
  436.       /* First put frame1 at innermost frame
  437.      and frame2 N levels up from there.  */
  438.       frame1 = get_current_frame ();
  439.       frame2 = frame1;
  440.       while (*level_offset_ptr < 0 && frame2 != frame)
  441.     {
  442.       frame2 = get_prev_frame (frame2);
  443.       (*level_offset_ptr) ++;
  444.     }
  445.       /* Then slide frame1 and frame2 up in synchrony
  446.      and when frame2 reaches our starting point
  447.      frame1 must be N levels down from there.  */
  448.       while (frame2 != frame)
  449.     {
  450.       frame1 = get_prev_frame (frame1);
  451.       frame2 = get_prev_frame (frame2);
  452.     }
  453.       return frame1;
  454.     }
  455.   return frame;
  456. }
  457.  
  458. /* The "frame" command.  With no arg, print selected frame briefly.
  459.    With arg LEVEL, select the frame at level LEVEL and print it.
  460.    With arg larger than 100000, use it as address of frame to select.
  461.    If from command file or user-defined command, don't print anything
  462.    if we have an argument.  */
  463.  
  464. static void
  465. frame_command (level_exp, from_tty)
  466.      char *level_exp;
  467.      int from_tty;
  468. {
  469.   register int i;
  470.   register FRAME frame;
  471.   unsigned int level, level1;
  472.  
  473.   if (level_exp)
  474.     {
  475.       level1 = level = parse_and_eval_address (level_exp);
  476.       if (level > 100000)
  477.     {
  478.       select_frame (level, -1);
  479.       frame_info (0);
  480.       return;
  481.     }
  482.  
  483.       frame = find_relative_frame (get_current_frame (), &level1);
  484.       if (level1 != 0)
  485.     error ("Stack level %d is out of range.", level);
  486.       select_frame (frame, level);
  487.       if (! from_tty)
  488.     return;
  489.     }
  490.  
  491.   print_stack_frame (selected_frame, selected_frame_level, 1);
  492. }
  493.  
  494. /* Select the frame up one or COUNT stack levels
  495.    from the previously selected frame, and print it briefly.  */
  496.  
  497. static void
  498. up_command (count_exp)
  499.      char *count_exp;
  500. {
  501.   register FRAME frame;
  502.   int count = 1, count1;
  503.   if (count_exp)
  504.     count = parse_and_eval_address (count_exp);
  505.   count1 = count;
  506.   
  507.   frame = find_relative_frame (selected_frame, &count1);
  508.   if (count1 != 0 && count_exp == 0)
  509.     error ("Initial frame selected; you cannot go up.");
  510.   select_frame (frame, selected_frame_level + count - count1);
  511.  
  512.   print_stack_frame (selected_frame, selected_frame_level, 1);
  513. }
  514.  
  515. /* Select the frame down one or COUNT stack levels
  516.    from the previously selected frame, and print it briefly.  */
  517.  
  518. static void
  519. down_command (count_exp)
  520.      char *count_exp;
  521. {
  522.   register FRAME frame;
  523.   int count = -1, count1;
  524.   if (count_exp)
  525.     count = - parse_and_eval_address (count_exp);
  526.   count1 = count;
  527.   
  528.   frame = find_relative_frame (selected_frame, &count1);
  529.   if (count1 != 0 && count_exp == 0)
  530.     error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
  531.   select_frame (frame, selected_frame_level + count - count1);
  532.  
  533.   print_stack_frame (selected_frame, selected_frame_level, 1);
  534. }
  535.  
  536. static void
  537. return_command (retval_exp, from_tty)
  538.      char *retval_exp;
  539.      int from_tty;
  540. {
  541.   struct symbol *thisfun = get_frame_function (selected_frame);
  542.  
  543.   /* If interactive, require confirmation.  */
  544.  
  545.   if (from_tty)
  546.     {
  547.       if (thisfun != 0)
  548.     {
  549.       if (!query ("Make %s return now? ", SYMBOL_NAME (thisfun)))
  550.         error ("Not confirmed.");
  551.     }
  552.       else
  553.     if (!query ("Make selected stack frame return now? "))
  554.       error ("Not confirmed.");
  555.     }
  556.  
  557.   /* Do the real work.  Pop until the specified frame is current.  */
  558.  
  559.   while (selected_frame != get_current_frame ())
  560.     POP_FRAME;
  561.  
  562.   /* Then pop that frame.  */
  563.  
  564.   POP_FRAME;
  565.  
  566.   /* Compute the return value (if any) and store in the place
  567.      for return values.  */
  568.  
  569.   if (retval_exp)
  570.     set_return_value (parse_and_eval (retval_exp));
  571.  
  572.   /* If interactive, print the frame that is now current.  */
  573.  
  574.   if (from_tty)
  575.     frame_command ("0", 1);
  576. }
  577.  
  578. void
  579. initialize_stack ()
  580. {
  581.   add_com ("return", class_stack, return_command,
  582.        "Make selected stack frame return to its caller.\n\
  583. Control remains in the debugger, but when you continue\n\
  584. execution will resume in the frame above the one now selected.\n\
  585. If an argument is given, it is an expression for the value to return.");
  586.  
  587.   add_com ("up", class_stack, up_command,
  588.        "Select and print stack frame that called this one.\n\
  589. An argument says how many frames up to go.");
  590.  
  591.   add_com ("down", class_stack, down_command,
  592.        "Select and print stack frame called by this one.\n\
  593. An argument says how many frames down to go.");
  594.   add_com_alias ("do", "down", class_stack, 1);
  595.  
  596.   add_com ("frame", class_stack, frame_command,
  597.        "Select and print a stack frame.\n\
  598. With no argument, print the selected stack frame.  (See also \"info frame\").\n\
  599. An argument specifies the frame to select.\n\
  600. It can be a stack frame number or the address of the frame.\n\
  601. With argument, nothing is printed if input is coming from\n\
  602. a command file or a user-defined command.");
  603.  
  604.   add_com_alias ("f", "frame", class_stack, 1);
  605.  
  606.   add_com ("backtrace", class_stack, backtrace_command,
  607.        "Print backtrace of all stack frames, or innermost COUNT frames.");
  608.   add_com_alias ("bt", "backtrace", class_stack, 0);
  609.   add_com_alias ("where", "backtrace", class_alias, 0);
  610.   add_info ("stack", backtrace_command,
  611.         "Backtrace of the stack, or innermost COUNT frames.");
  612.   add_info_alias ("s", "stack", 1);
  613.   add_info ("frame", frame_info,
  614.         "All about selected stack frame, or frame at ADDR.");
  615.   add_info_alias ("f", "frame", 1);
  616.   add_info ("locals", locals_info,
  617.         "Local variables of current stack frame.");
  618.   add_info ("args", args_info,
  619.         "Argument variables of current stack frame.");
  620. }
  621.